﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZSZ.AdminWeb.App_Start;
using ZSZ.AdminWeb.Models.AdminUser;
using ZSZ.CommonMvc;
using ZSZ.Iservice;

namespace ZSZ.AdminWeb.Controllers
{
    public class AdminUserController : Controller
    {
        public IRoleService RoleService { get; set; }
        public IAdminUserService AdminUserService { get; set; }
        public ICityService CityService { get; set; }
        [CheckPermission("Admin.List")]
        public ActionResult List()
        {
            var amins = AdminUserService.GetAll();
            return View(amins);
        }
        [HttpPost]
        [CheckPermission("Admin.Delete")]
        public ActionResult DeletedById(long id)
        {
            AdminUserService.MarkDeleted(id);
            return Json(new JsonCommon { Status = "ok" });
        }
        [HttpPost]
        [CheckPermission("Admin.Delete")]
        public ActionResult DeleteByIds(long[] adminUserIds)
        {
            foreach (var item in adminUserIds)
            {
                AdminUserService.MarkDeleted(item);
            }
            return Json(new JsonCommon { Status = "ok" });
        }
        [HttpGet]
        [CheckPermission("Admin.Add")]
        public ActionResult Add()
        {
            var roles = RoleService.GetAll();
            var cities = CityService.GetAll();
            AdminUserGetAddModel model = new AdminUserGetAddModel();
            model.Cities = cities;
            model.Roles = roles;
            return View(model);
        }
        [HttpPost]
        [CheckPermission("Admin.Add")]
        public ActionResult Add(AdminUserPostAddModel model)
        {
            if (!ModelState.IsValid)
            {
                return Json(new JsonCommon { Status = "err", ErrorMsg = "请按照规矩填写" });
            }
            long? cityId = model.cityId;
            if (model.cityId == 0)
            {
                cityId = null;
            }
            long id = AdminUserService.AddAdminUser(model.Name, model.PhoneNum, model.Pass, model.Email, cityId);
            RoleService.UpdateRoleIds(id, model.RoleIds);
            return Json(new JsonCommon { Status = "ok" });
        }
        [HttpGet]
        [CheckPermission("Admin.Edit")]
        public ActionResult Edit(long id)
        {
            var adminDto = AdminUserService.GetById(id);
            var roleAll = RoleService.GetAll();
            var xuanRole = RoleService.GetByAdminUserId(id);
            var cityAll = CityService.GetAll();
            AdminUserGetEditModel model = new AdminUserGetEditModel();
            model.AdminUser = adminDto;
            model.RoleAll = roleAll;
            model.RoleXuan = xuanRole;
            model.CityAll = cityAll;
            return View(model);
        }
        [HttpPost]
        [CheckPermission("Admin.Edit")]
        public ActionResult Edit(EditPostModel model)
        {
            if (!ModelState.IsValid)
            {
                return Json(new JsonCommon { Status = "err", ErrorMsg = "请按照规矩填写" });
            }
            long? cityId = model.CityId;
            if (model.CityId == 0)
            {
                cityId = null;
            }
            AdminUserService.UpdateAdminUser(model.AdminUserId, model.Name, model.Pass, model.Email, cityId);
            RoleService.UpdateRoleIds(model.AdminUserId, model.RoleIds);
            return Json(new JsonCommon { Status = "ok" });
        }
    }
}